from goods.models import GoodsChannel
from rest_framework.pagination import PageNumberPagination


def get_categories():
    categories = {}
    channels = GoodsChannel.objects.order_by('group_id', 'sequence')
    for channel in channels:
        group_id = channel.group_id  # 当前组

        if group_id not in categories:
            categories[group_id] = {'channels': [], 'sub_cats': []}

        cat1 = channel.category  # 当前频道的类别

        # 追加当前频道
        categories[group_id]['channels'].append({
            'id': cat1.id,
            'name': cat1.name,
            'url': channel.url
        })
        # 构建当前类别的子类别
        for cat2 in cat1.goodscategory_set.all():
            cat2.sub_cats = []
            for cat3 in cat2.goodscategory_set.all():
                cat2.sub_cats.append(cat3)
            categories[group_id]['sub_cats'].append(cat2)

    return categories


class StandardResultsSetPagination(PageNumberPagination):
    # 后端决定每页有几个数据
    # page_size = 2
    # 前端规定每页有几个数据
    page_size_query_param = 'page_size'
    max_page_size = 10
